package 百度;

public class _28_实现_strStr {
    public int strStr(String haystack, String needle) {
        if (haystack.length() == 0) return needle.length() == 0 ? 0 : -1;
        if (needle.length() == 0) return 0;
        char[] arrH = haystack.toCharArray();
        char[] arrN = needle.toCharArray();
        if (arrH.length < arrN.length) return -1;
        int res = -1;
        for (int i = 0; i <= arrH.length - arrN.length; i++) {
            boolean flag = false;
            for (int j = 0; j < arrN.length; j++) {
                if (arrH[i + j] != arrN[j]) {
                    break;
                }
                if (j == arrN.length - 1) {
                    flag = true;
                    res = i;
                }

            }
            if (flag) {
                return res;
            }
        }
        return -1;
    }
}
